package com.blb.zy_community.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.zy_community.entity.ResponseResult;
import com.blb.zy_community.entity.SysUser;
import com.blb.zy_community.entity.ZyOwner;
import com.blb.zy_community.service.ISysUserService;
import com.blb.zy_community.service.IZyOwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Security需要的用户登录逻辑实现类
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private ISysUserService userService;
    @Autowired
    private IZyOwnerService ownerService;

    @Autowired
    private BCryptPasswordEncoder encoder;


    /**
     * 通过用户名查找用户
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        //判断用户名是管理员还是业主
        if (username.contains("-owner")) {
//            //截取业主登录用户名
//            String ownerRealName = username.split("-")[0];
            //通过用户名查找业主
            ZyOwner owner = ownerService.getOne(new QueryWrapper<ZyOwner>().lambda().eq(ZyOwner::getOwnerRealName, username));
            if(owner == null ){
                throw new UsernameNotFoundException("用户名不存在");
            }
            if (!"Enable".equals(owner.getOwnerStatus())){
                throw new UsernameNotFoundException("业主账号已被停用，请联系管理员");
            }
//            List<GrantedAuthority> authorities = new ArrayList<>();
            List<GrantedAuthority> authorities = AuthorityUtils.commaSeparatedStringToAuthorityList("");

            org.springframework.security.core.userdetails.User userDetails =
                    new org.springframework.security.core.userdetails.User(username,owner.getOwnerPassword(),authorities);
            return userDetails;
        } else {
            //要通过用户名查找用户
            SysUser user = userService.getOne(new QueryWrapper<SysUser>().lambda().eq(SysUser::getUserName, username));
            if(user == null){
                throw new UsernameNotFoundException("用户名不存在");
            }
            if (!"0".equals(user.getStatus())){
                throw new UsernameNotFoundException("用户已被停用，请联系管理员");
//                return ResponseResult.error(403L,"用户已被停用，请联系管理员");
            }
            //查询所有的角色和权限
            List<GrantedAuthority> authorities = userService.getAuto(username);
            org.springframework.security.core.userdetails.User userDetails =
                    new org.springframework.security.core.userdetails.User(username,user.getPassword(),authorities);
            return userDetails;
        }
    }
}
